package com.apestech.api.xml.esb.modul;

import com.apestech.api.condition.ConFieldM;
import com.apestech.api.xml.esb.EsbUtil;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.LocalVariable;
import com.apestech.framework.util.callback.JSONMapKeyInter;
import com.apestech.framework.util.callback.SpiltStrCallback;
import com.apestech.framework.xml.condition.modul.Condition;
import com.apestech.framework.xml.condition.modul.Dimension;
import com.apestech.framework.xml.plugin.modul.Model;
import com.apestech.framework.xml.plugin.modul.SpecialField;
import com.apestech.framework.xml.plugin.modul.SysCondition;
import com.apestech.framework.xml.table.modul.Tables;
import org.elasticsearch.common.recycler.Recycler;

import java.util.ArrayList;

public class EsbFormPop extends EsbFormPub {
    private SqlSession session;
    private String theme;
    private String id;
    private String type;
    private JSONObject input;
    private SysCondition sysCondition;
    private boolean esb=false;
    private Table tab;
    private String purpose;

    public EsbFormPop(SqlSession session, String id, JSONObject input) throws Exception {
        int pos=id.lastIndexOf("_");
        if(pos<0){
            this.id=id;
            type="zd";
        } else {
            this.id=id.substring(0, pos);
            type=id.substring(pos+1, id.length());
        }
        if("cx".equals(type)){
            purpose="pp_search";
        } else {
            purpose="pp_operate";
        }
        this.session=session;
        theme=session.getTheme();
        this.input=input;
    }

    public void setEsb(boolean esb) {
        this.esb = esb;
    }

    public JSONObject getSearchForm() throws Exception {
        JSONObject param=new JSONObject();
        JSONObject modul= EsbUtil.getDivDef("search", theme);
        JSONObject field=null;
        tab=null;
        JSONObject in=EsbUtil.getPop(id);
        sysCondition= LocalVariable.getConfig().getSysCondition(session);
        if("strategy.queryTab".equals(id)) {
            if (in == null) throw new Exception("找不到该定义[" + id + "]配置信息，请检查!");
            param.put("id", id);
            param.put("title", in.getString("title"));
            param.put("rowSelection", in.getString("rowSelection"));
//            param.put("api", "pub.queryForm");
//            param.put("secondaryApi", "strategy.queryTab");
            param.put("rowModelType", "clientSide");
            JSONObject domain = new JSONObject();
            domain.put("table_id", "condition");
            param.put("domain", domain);
            setConField(in, param);
        } else {
            id=id.substring(id.lastIndexOf(".")+1,id.length());
            field=sysCondition.getDimensionField(theme+"."+id);
            String tabname=field.getString("foreignTab");
            if(in!=null){
                param.put("title", in.getString("title"));
                param.put("rowSelection", in.getString("rowSelection"));
                param.put("api", in.getString("api"));
            } else {
                param.put("title", "选择"+field.getString("comment"));
                param.put("rowSelection","single");
            }
            tab=session.getSqlTable(field.getString("sql"));
            setTabField(tab, field, param, in);
        }
        if(param.get("api")==null) param.put("api","pub.queryForm");
        if(param==null) return null;
        return getOutJson(modul, param, theme, new JSONMapKeyInter(){
            @Override
            public Object getValue(String key, Object value) throws Exception {
                return null;
            }
        });
    }

    private void setConField(JSONObject in, JSONObject param) throws Exception {
        JSONArray search= (JSONArray) in.get("search");
        JSONArray table=null;
        if(in.get("table") instanceof JSONArray){
            table= (JSONArray) in.get("table");
        } else {
            table=(JSONArray)((JSONObject)in.get("table")).get("field");
        }
        JSONObject searchControl= JSON.toJSON(in.get("searchControl"));
        JSONObject tableControl=JSON.toJSON(in.get("tableControl"));
        JSONArray con=new JSONArray();
        JSONArray gcon=new JSONArray();
        setSearchFieldList("grid_query", table, gcon, tableControl);
        setSearchFieldList("bill_query", search, con, searchControl);
        param.put("columnDefs", gcon);
        param.put("conditionField", con);
    }

    private void setSearchFieldList(String type, JSONArray list, JSONArray con, JSONObject fieldJson) throws Exception {
        int len=list.size();
        for(int i=0; i<len; i++){
            JSONObject m=list.get(i);
            String fid=m.getString("name");
            String label=m.getString("label");
//            JSONObject field=sysCondition.getControlField("esb."+fid);
//            boolean foreign=false;
//            if(field==null) field=sysCondition.getDimensionField("esb."+fid);
//            if(field==null){
//                Model model= LocalVariable.getConfig().getModel();
//                field=model.getAllField("esb."+fid);
//                if(field==null){
//                    if("Z_ID".equals(fid)){
//                        field=model.getAllField("esb.ID");
//                    }
//                }
//                if(field==null) new Exception("没有找到该字段[esb."+fid+"]定义，请检查代码！");
//            } else foreign=true;
//            String fkey=field.getString("foreignKey");
//            if(field.get("table")!=null) fkey=field.getString("field");
            ConFieldM conFieldM=new ConFieldM(fid, tab, session, purpose, "A", m);
            setSingleField(fid, conFieldM, type, label, con, m, true);
        }
    }

    private void setTabField(Table tab, JSONObject field, JSONObject param, JSONObject in) throws Exception {
//        String cols=tab.getColumn();
//        String[] clist=cols.split(",");
        JSONArray con=new JSONArray();
        JSONArray gcon=new JSONArray();
//        JSONObject result=new JSONObject();
//        int clen=clist.length;
//        for(int i=0; i<clen; i++){  //查询结果
//            String k=clist[i];
//            if(!"".equals(k)) {
//                JSONObject m=new JSONObject();
//                m.put("dbtype", tab.getDbType(k));
//                m.put("noNull", "true".equals(tab.getNotNull(k)));
//                result.put(k, m);
//            }
//        }
        //in=null;
        String k=null;
        String kname=null;
        if(in!=null){
            JSONArray search=JSON.toJSONArray(in.get("search"));
            JSONArray table=JSON.toJSONArray(in.get("table"));
            JSONObject searchControl=JSON.toJSON(in.get("searchControl"));
            JSONObject tableControl=JSON.toJSON(in.get("tableControl"));
            int len=search.size();
            for(int i=0; i<len; i++){
                JSONObject m=search.get(i);
                k=m.getString("name");
                kname=m.getString("label");
                ConFieldM conFieldM=new ConFieldM(k, tab, session, purpose, "A", m);
                setSingleField(k, conFieldM,"bill_query", kname, con, searchControl, false);
            }
            len=table.size();
            for(int i=0; i<len; i++){
                JSONObject m=table.get(i);
                k=m.getString("name");
                kname=m.getString("label");
                ConFieldM conFieldM=new ConFieldM(k, tab, session, purpose, "A", m);
                setSingleField(k, conFieldM,"grid_query", kname, gcon, tableControl, false);
            }
        } else {
            String sname=field.getString("auxiliaryField");
            String cname=field.getString("auxiliaryName");
            String qcon=field.getString("qryCon");
            String id=field.getString("field");
            String idname="编码";
            if(cname==null){
                cname=field.getString("comment");
            }
            ConFieldM conFieldM=new ConFieldM(id, tab, session, purpose, "A", field);
            setSingleField(id, conFieldM,"grid_query", idname, gcon, field, false);
            setSingleField(id, conFieldM,"bill_query", idname, con, field, false);
            if(sname.indexOf(",")>=0){
                String[] sf=sname.split(",");
                String[] cf=cname.split(",");
                int len=sf.length;
                for(int i=0; i<len; i++){
                    String fid=sf[i];
                    String fname=cf[i];
                    conFieldM=new ConFieldM(fid, tab, session, purpose, "A", null);
                    setSingleField(fid, conFieldM,"grid_query", fname, gcon, null, false);
                    if((","+qcon+",").indexOf(","+fid+",")>=0){
                            setSingleField(fid, conFieldM,"bill_query", fname, con, null, false);
                    }
                }
            } else {
                conFieldM=new ConFieldM(sname, tab, session, purpose, "A", null);
                setSingleField(sname, conFieldM,"bill_query", cname, con, null, false);
                setSingleField(sname, conFieldM,"grid_query", cname, gcon, null, false);
            }
        }
        param.put("columnDefs", gcon);
        param.put("conditionField", con);
    }

    public JSONObject getConditionQry(Condition condition, String mid, JSONObject fieldJson) throws Exception {  //以CONDITION为驱动对象，获取对应查询条件和查询结果字段列表
        String tabId=condition.getTableKey().getTabName(mid);
        String mxTab=tabId+"_ITEM";
        condition.getTables();
        Tables tabs=condition.getTablesById(tabId);
        if(tabs==null){
            throw new Exception("条件["+condition.getId()+"]维度组合["+mid+"]不存在，请重新选择维度数据！");
        }
        Tables.Table tab=tabs.getTable(tabId);
        Tables.Table mtab=tabs.getTable(mxTab);
        String unVisible="";  //后期补
        if(fieldJson==null){
            fieldJson=new JSONObject();
        }
        JSONArray conf=new JSONArray();
        JSONArray tabf=new JSONArray();
        SpecialField.Field sp=session.getSpecialField();
        EsbFormConditionField field;
        ArrayList<Tables.Field> flist=tab.getFields();
        int len=flist.size();
        for(int i=0; i<len; i++){
            Tables.Field f=flist.get(i);
            String k=f.getId();
            String s=theme+"."+k;
            if("VERSION".equals(k) || "INNER_BUSINESS_TYPE".equals(k) || "IMP_ID".equals(k) ) {
                continue;
            }
            if(sp.getScene().equals(k)){
                continue;
            }
            if(sp.getTimestamp().equals(k)){
                continue;
            }
            if(sp.getConditionId().equals(k)){
                ConFieldM conFieldM=new ConFieldM("Z_ID", session.getDictTable(tabId), session, "","A", fieldJson);
//                field=new EsbFormConditionField("策略编码","Z_ID",f.getForeignKey(),f.isNotNull(),f.getType(),
//                        f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                        "bill_oper", theme, null, fieldJson, sysCondition);
                field=new EsbFormConditionField("bill_oper", "cx", conFieldM,"Z_ID", input);
                field.setComment("策略编码");
                JSONObject tf=field.getField();
                tf.put("key", f.getId());
                tabf.add(getGridJson(tf, theme));

                conFieldM=new ConFieldM(k, session.getDictTable(tabId), session, "","A", fieldJson);
                field=new EsbFormConditionField("bill_oper", "cx", conFieldM,k, input);
//                field=new EsbFormConditionField(unVisible,f.getComment(),k,f.getForeignKey(),f.isNotNull(),f.getType(),
//                        f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                        true, false, theme, null, fieldJson, sysCondition);
                tf=field.getField();
                tf.put("key", f.getId());
                tabf.add(getGridJson(tf, theme));
            } else {
                if(!sp.getStopId().equals(k) && !sp.getCreateId().equals(k) && !sp.getApproveId().equals(k) && !sp.getUpdateId().equals(k)
                        && !"CONTROL_TYPE".equals(k) && !"PRIORITY".equals(k) && !"UPDATE_VALUE".equals(k)
                        && !sp.getCreateDate().equals(k)  && !sp.getStopDate().equals(k)  && !sp.getApproveDate().equals(k)
                        && !sp.getStartDate().equals(k) && !sp.getEndDate().equals(k) ){

//                    field=new EsbFormConditionField(unVisible,f.getComment(),k,f.getForeignKey(),f.isNotNull(),f.getType(),
//                            f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                            true, true, theme, null, fieldJson, sysCondition);  //input null的值需要重新处理
//                    JSONObject cf=field.getField();
//                    cf.put("key", f.getId());
//                    conf.add(getFormJson(cf, theme));
                    //sysCondition.getDimensionField(s)!=null
                }
//                field=new EsbFormConditionField(unVisible,f.getComment(),k,f.getForeignKey(),f.isNotNull(),f.getType(),
//                        f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                        true, false, theme, null, fieldJson, sysCondition);
//                JSONObject tf=field.getField();
//                tf.put("key", f.getId());
//                tabf.add(getGridJson(tf, theme));
            }
        }

        flist=mtab.getFields();
        len=flist.size();
        for(int i=0; i<len; i++){
            Tables.Field f=flist.get(i);
            String k=f.getId();
            String s=theme+"."+k;
            if(sysCondition.getDimensionField(s)!=null){
//                field=new EsbFormConditionField(unVisible,f.getComment(),k,f.getForeignKey(),f.isNotNull(),f.getType(),
//                        f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                        true, true, theme, null, fieldJson, sysCondition);
//                JSONObject cf=field.getField();
//                conf.add(getFormJson(cf, theme));
            }
            if(!"billpop".equals(input.get("query"))){
//                field=new EsbFormConditionField(unVisible,f.getComment(),k,f.getForeignKey(),f.isNotNull(),f.getType(),
//                        f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                        true, false, theme, null, fieldJson, sysCondition);
//                JSONObject tf=field.getField();
//                tabf.add(getGridJson(tf, theme));
            }
        }
        JSONObject r=new JSONObject();
        r.put("table", tabf);
        r.put("con", conf);
        return r;
    }

    public JSONObject getConditionEQry(Condition condition, String mid, JSONObject fieldJson) throws Exception {  //以CONDITION为驱动对象，获取对应查询条件和查询结果字段列表
        String tabId=condition.getTableKey().getTabName(mid);
        String eTab=tabId+"_EXEC";
        condition.getTables();
        Tables tabs=condition.getTablesById(tabId);
        if(tabs==null){
            throw new Exception("条件["+condition.getId()+"]维度组合["+mid+"]不存在，请重新选择维度数据！");
        }
        Tables.Table etab=tabs.getTable(eTab);
        String unVisible="";  //后期补
        if(fieldJson==null){
            fieldJson=new JSONObject();
        }
        JSONArray conf=new JSONArray();
        JSONArray tabf=new JSONArray();
        SpecialField.Field sp=session.getSpecialField();
        EsbFormConditionField field;
        ArrayList<Tables.Field> flist=etab.getFields();
        int len=flist.size();
        for(int i=0; i<len; i++){
            Tables.Field f=flist.get(i);
            String k=f.getId();
            String s=theme+"."+k;
            if("VERSION".equals(k) || "INNER_BUSINESS_TYPE".equals(k)) {
                continue;
            }
            if(sp.getScene().equals(k)){
                continue;
            }
            if(sp.getTimestamp().equals(k)){
                continue;
            }
            if(sp.getConditionId().equals(k)){
                ConFieldM conFieldM=new ConFieldM("Z_ID", session.getDictTable(tabId), session, "", "A", fieldJson);
                field=new EsbFormConditionField("bill_oper", "cx", conFieldM, "Z_ID", null);
                field.setComment("策略编码");
//                field=new EsbFormConditionField("策略编码","Z_ID",f.getForeignKey(),f.isNotNull(),f.getType(),
//                        f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                        "bill_oper", theme, null, fieldJson, sysCondition);
                JSONObject tf=field.getField();
                tf.put("key", f.getId());
                tabf.add(getGridJson(tf, theme));

                conFieldM=new ConFieldM(k, session.getDictTable(tabId), session, "", "A", fieldJson);
                field=new EsbFormConditionField("bill_oper", "cx", conFieldM, k, null);
//                field=new EsbFormConditionField(unVisible,f.getComment(),k,f.getForeignKey(),f.isNotNull(),f.getType(),
//                        f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                        true, false, theme, null, fieldJson, sysCondition);
                tf=field.getField();
                tf.put("key", f.getId());
                tabf.add(getGridJson(tf, theme));
            } else {
                if(!sp.getStopId().equals(k) && !sp.getCreateId().equals(k) && !sp.getApproveId().equals(k) && !sp.getUpdateId().equals(k)
                        && !"CONTROL_TYPE".equals(k) && !"PRIORITY".equals(k) && !"UPDATE_VALUE".equals(k)
                        && !sp.getCreateDate().equals(k)  && !sp.getStopDate().equals(k)  && !sp.getApproveDate().equals(k)
                        && !sp.getStartDate().equals(k) && !sp.getEndDate().equals(k) && sysCondition.getDimensionField(s)!=null){
//                    field=new EsbFormConditionField(unVisible,f.getComment(),k,f.getForeignKey(),f.isNotNull(),f.getType(),
//                            f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                            true, true, theme, null, fieldJson, sysCondition);  //input null的值需要重新处理
//                    JSONObject cf=field.getField();
//                    cf.put("key", f.getId());
//                    conf.add(getFormJson(cf, theme));
                    //sysCondition.getDimensionField(s)!=null
                }
//                field=new EsbFormConditionField(unVisible,f.getComment(),k,f.getForeignKey(),f.isNotNull(),f.getType(),
//                        f.getForeignKey()!= null && (f.getForeignTab()==null || !f.getForeignTab().equals(tabs.getMasterTable())),
//                        true, false, theme, null, fieldJson, sysCondition);
//                JSONObject tf=field.getField();
//                tf.put("key", f.getId());
//                tabf.add(getGridJson(tf, theme));
            }
        }

        JSONObject r=new JSONObject();
        r.put("table", tabf);
        r.put("con", conf);
        return r;
    }

    //场景：仅只查主表的查找窗体，明细表里的关键字段可为查询条件
    public JSONObject getConditionQry_esb(Condition condition, String mid, JSONObject fieldJson) throws Exception {  //以CONDITION为驱动对象，获取对应查询条件和查询结果字段列表
        String query=input.getString("query");
        String tabId=condition.getTableKey().getTabName(mid);
        Table ztab=session.getDictTable(tabId);
        Table tabmx=session.getDictTable(tabId+"_ITEM");
        if(ztab==null){
            throw new Exception("条件["+condition.getId()+"]维度组合["+mid+"]不存在，请重新选择维度数据！");
        }
        if(fieldJson==null){
            fieldJson=new JSONObject();
        }
        ArrayList<String> cxcon=getOrderCxCon();
        ArrayList<String> cxgrid=getOrderCx();
        JSONArray conf=new JSONArray();
        JSONArray tabf=new JSONArray();
        EsbFormConditionField field=null;
        for(String k : cxcon){
            if("{DEF}".equals(k)){
                Dimension d=condition.getDimensionById(mid);
                execSplitStr(d.getFieldKey(), ",", new SpiltStrCallback() {
                    @Override
                    public void exec(String id) throws Exception {
                        ConFieldM conFieldM=null;
                        if(tabmx.isField(id)){
                            conFieldM=new ConFieldM(id, tabmx, session, "", "A", null);
                        } else {
                            conFieldM=new ConFieldM(id, ztab, session, "", "A", null);
                        }
                        EsbFormConditionField field=new EsbFormConditionField("bill_query", "zd", conFieldM, id, null);
                        field.setEsb(true);
                        JSONObject cf=field.getField();
                        cf.put("key", k);
                        conf.add(cf);

                        JSONObject fieldD=conFieldM.getFieldD(id);
                        String conqry=fieldD.getString("conqry");
                        ConFieldM.TableData tabd=conFieldM.getTableData();
                        for(String id1:tabd.getChildKey()){
                            String id2=id1.replace(id+"_","");
                            if(id1.indexOf("_NAME")<0 && id1.indexOf("_DESCRIPTION")<0 && conqry.indexOf(id2)>=0) {
                                field=new EsbFormConditionField("bill_query", "zd", conFieldM, id1, null);
                                field.setEsb(true);
                                cf=field.getField();
                                cf.put("key", id1);
                                conf.add(cf);
                            }
                        }
                    }
                });

            } else {
                if("Z_ID".equals(k)){
                    ConFieldM conFieldM=new ConFieldM(k, ztab, session, "", "A", fieldJson);
                    field=new EsbFormConditionField("bill_query", "zd", conFieldM, k, null);
                    field.setComment("策略编码");
                    field.setEsb(true);
                    JSONObject cf=field.getField();
                    cf.put("key", k);
                    conf.add(cf);
                } else {
                    if(ztab.isField(k)){
                        ConFieldM conFieldM=new ConFieldM(k, ztab, session, "", "A", fieldJson);
                        field=new EsbFormConditionField("bill_query", "zd", conFieldM, k, fieldJson);
                        field.setEsb(true);
                        JSONObject cf=field.getField();
                        cf.put("key", k);
                        conf.add(cf);
                    }
                }

            }
        }

        for(String k : cxgrid){
            if("{DEF}".equals(k)){
                Dimension d=condition.getDimensionById(mid);
                execSplitStr(d.getFieldKey(), ",", new SpiltStrCallback() {
                    @Override
                    public void exec(String id) throws Exception {
                        ConFieldM conFieldM=null;
                        if(ztab.isField(id)){
                            conFieldM=new ConFieldM(id, ztab, session, "", "A", null);
                            EsbFormConditionField field=new EsbFormConditionField("grid_query", "zd", conFieldM, id, null);
                            field.setEsb(true);
                            JSONObject tf=field.getField();
                            tf.put("key", id);
                            tabf.add(tf);
                        } else  if("bill".equals(query)){
                            if(tabmx.isField(id)){
                                conFieldM=new ConFieldM(id, tabmx, session, "", "B", null);
                                EsbFormConditionField field=new EsbFormConditionField("grid_query", "zd", conFieldM, id, null);
                                field.setEsb(true);
                                JSONObject tf=field.getField();
                                tf.put("key", id);
                                tabf.add(tf);

                                JSONObject fieldD=conFieldM.getFieldD(id);
                                if(fieldD!=null){
                                    String display=fieldD.getString("display");
                                    for(String id1:conFieldM.getTableData().getChildKey()){
                                        String id2=id1.replace(id+"_","");
                                        if(id1.indexOf("_NAME")<0 && id1.indexOf("_DESCRIPTION")<0 && display.indexOf(id2)>=0) {
                                            field=new EsbFormConditionField("bill_query", "zd", conFieldM, id1, null);
                                            field.setEsb(true);
                                            tf=field.getField();
                                            tf.put("key", id1);
                                            tabf.add(tf);
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            } else {
                if("Z_ID".equals(k)){
                    ConFieldM conFieldM=new ConFieldM("Z_ID", ztab, session, "", "A", fieldJson);
                    field=new EsbFormConditionField("grid_query", "zd", conFieldM, "Z_ID", null);
                    field.setComment("策略编码");
                    field.setEsb(true);
                    JSONObject tf=field.getField();
                    tf.put("key", k);
                    tabf.add(tf);
                } else {
                    if(ztab.isField(k)){
                        ConFieldM conFieldM=new ConFieldM(k, ztab, session, "", "A", fieldJson);
                        field=new EsbFormConditionField("grid_query", "zd", conFieldM, k, fieldJson);
                        field.setEsb(true);
                        JSONObject tf=field.getField();
                        tf.put("key", k);
                        tabf.add(tf);
                    }
                }
            }
        }

        JSONObject r=new JSONObject();
        r.put("detail", tabf);
        r.put("master", conf);
        return r;
    }

    public JSONObject getConditionEQry_esb(Condition condition, String mid, JSONObject fieldJson) throws Exception {  //以CONDITION为驱动对象，获取对应查询条件和查询结果字段列表
        String tabId=condition.getTableKey().getTabName(mid);
        String eTab=tabId+"_EXEC";
        condition.getTables();
        Tables tabs=condition.getTablesById(tabId);
        if(tabs==null){
            throw new Exception("条件["+condition.getId()+"]维度组合["+mid+"]不存在，请重新选择维度数据！");
        }
        Table etab=session.getDictTable(eTab);
        JSONArray conf=new JSONArray();
        JSONArray tabf=new JSONArray();
        EsbFormConditionField field=null;
        ConFieldM conFieldM=null;

        conFieldM=new ConFieldM("Z_ID", etab, session, "", "A", fieldJson);
        field=new EsbFormConditionField("bill_query", "cx", conFieldM, "Z_ID", null);
        field.setComment("策略编码");
        field.setEsb(true);
        JSONObject tf=field.getField();
        tf.put("key", "Z_ID");
        conf.add(tf);

        field=new EsbFormConditionField("grid_query", "cx", conFieldM, "Z_ID", null);
        field.setComment("策略编码");
        field.setEsb(true);
        JSONObject cf=field.getField();
        cf.put("key", "Z_ID");
        tabf.add(cf);

        ArrayList<String> cxcon=getOrderCxCon();
        ArrayList<String> flist=etab.getColumnList();
        int len=flist.size();
        for(int i=0; i<len; i++){
            String k=flist.get(i);
            if(!isInnerField(k, "cx", true)){
                conFieldM=new ConFieldM(k, etab, session, "", "A", fieldJson);
                field=new EsbFormConditionField("grid_query", "cx", conFieldM, k, null);
                field.setEsb(true);
                cf=field.getField();
                cf.put("key", k);
                tabf.add(cf);

                JSONObject fieldD=conFieldM.getFieldD(k);
                if(fieldD!=null){
                    String display=fieldD.getString("display");
                    for(String id1:conFieldM.getTableData().getChildKey()){
                        String id2=id1.replace(k+"_","");
                        if(id1.indexOf("_NAME")<0 && id1.indexOf("_DESCRIPTION")<0 && display.indexOf(id2)>=0) {
                            field=new EsbFormConditionField("bill_query", "zd", conFieldM, id1, null);
                            field.setEsb(true);
                            cf=field.getField();
                            cf.put("key", id1);
                            tabf.add(cf);
                        }
                    }
                }
            }
        }
        for(String k:cxcon){
            if(k.equals("{DEF}")){
                Dimension d=condition.getDimensionById(mid);
                execSplitStr(d.getFieldKey(), ",", new SpiltStrCallback() {
                    @Override
                    public void exec(String id) throws Exception {
                        ConFieldM conFieldM=null;
                        if(etab.isField(id)){
                            conFieldM=new ConFieldM(id, etab, session, "", "A", null);
                            EsbFormConditionField field=new EsbFormConditionField("bill_query", "zd", conFieldM, id, null);
                            field.setEsb(true);
                            JSONObject tf=field.getField();
                            tf.put("key", id);
                            conf.add(tf);
                        }
                        JSONObject fieldD=conFieldM.getFieldD(id);
                        String conqry=fieldD.getString("conqry");
                        for(String id1:conFieldM.getTableData().getChildKey()){
                            String id2=id1.replace(id+"_","");
                            if(id1.indexOf("_NAME")<0 && id1.indexOf("_DESCRIPTION")<0 && conqry.indexOf(id2)>=0) {
                                EsbFormConditionField field=new EsbFormConditionField("bill_query", "zd", conFieldM, id1, null);
                                field.setEsb(true);
                                JSONObject tf=field.getField();
                                tf.put("key", id1);
                                conf.add(tf);
                            }
                        }
                    }
                });
            } else {
                if(etab.isField(k)){
                    conFieldM=new ConFieldM(k, etab, session, "", "A", fieldJson);
                    field=new EsbFormConditionField("bill_query", "cx", conFieldM, k, null);
                    field.setEsb(true);
                    if(k.equals("Z_ID")){
                        field.setComment("策略编码");
                    }
                    tf=field.getField();
                    tf.put("key", k);
                    conf.add(tf);
                }
            }
        }

        JSONObject r=new JSONObject();
        r.put("detail", tabf);
        r.put("master", conf);
        return r;
    }

}
